#include "testwidget.h"
#include "ui_testwidget.h"

TestWidget::TestWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestWidget)
{
    ui->setupUi(this);

    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    this->setWindowTitle("测试");
    initWidget();
    m_item = new QTreeWidgetItem(newTree);
}

TestWidget::~TestWidget()
{
    delete ui;
}
void TestWidget::initWidget()
{
    QLabel *label;
    label = new QLabel("搜索描述:",this);
    label->setGeometry(0,0,100,40);
    label->setStyleSheet("background-color:rgb(0,0,0);color:white;font-size:16px;");

    label = new QLabel("字节:",this);
    label->setGeometry(350,0,40,40);
    label->setStyleSheet("background-color:rgb(0,0,0);color:white;font-size:16px;");

    desLineedit = new QLineEdit(this);
    desLineedit->setGeometry(90,5,100,30);
    desLineedit->setStyleSheet("border:1px solid white;background-color:rgb(0,0,0);color:white;font-size:16px;");
    connect(desLineedit,SIGNAL(textChanged(QString)),this,SLOT(slot_searchText(QString)));

    byteLineedit = new QLineEdit(this);
    byteLineedit->setGeometry(390,5,60,30);
    byteLineedit->setStyleSheet("border:1px solid white;background-color:rgb(0,0,0);color:white;font-size:16px;");
    connect(byteLineedit,SIGNAL(textChanged(QString)),this,SLOT(slot_searchText(QString)));

    newTree = new QTreeWidget(this);
    newTree->setStyleSheet("QTreeWidget{background-color:rgb(0,0,0);color:rgb(255,255,255);}\
                            QHeaderView::section{background-color:rgb(0,0,0);color:rgb(255,255,255);\
                            border:none;font:14pt}");
    newTree->headerItem()->setText(0,QString());
    newTree->setGeometry(0,40,this->width(),this->height()-40);

    QStringList headers;
    headers<<"类型"<<"描述"<<"字节类型"<<"序号"<<"字节"<<"位"<<"值";
    newTree->setHeaderLabels(headers);
    QTreeWidgetItem *group[GlobalConfig::recvTRDPList.length()];
    for(int i=0;i<GlobalConfig::recvTRDPList.length();++i)
    {
        group[i] = new QTreeWidgetItem(newTree);
        group[i]->setText(0,QString().setNum(GlobalConfig::recvTRDPList.at(i).comid));
    }
    for(int i=0;i<GlobalConfig::recvTRDPList.length();++i)
    {
        for(int j=0;j<GlobalConfig::recvTRDPList[i].node.length();++j)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem(group[i]);
            item->setText(0,"");
            item->setText(1,GlobalConfig::recvTRDPList[i].node.at(j).description);
            switch (GlobalConfig::recvTRDPList[i].node.at(j).size) {
            case 1:
                item->setText(2,"BOOLEAN1");
                break;
            case 8:
                item->setText(2,"UNSIGNED8");
                break;
            case 16:
                item->setText(2,"UNSIGNED16");
                break;
            case 32:
                item->setText(2,"UNSIGNED32");
                break;
            default:
                break;
            }
            item->setText(3,GlobalConfig::recvTRDPList[i].node.at(j).index);
            item->setText(4,QString().setNum(GlobalConfig::recvTRDPList[i].node.at(j).byte));
            item->setText(5,QString().setNum(GlobalConfig::recvTRDPList[i].node.at(j).bit));
            item->setText(6,"0");
        }
    }
    connect(newTree,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(slot_valueChange(QTreeWidgetItem*,int)));
    connect(newTree,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(slot_itemDoubleClicked(QTreeWidgetItem*,int)));
    connect(newTree,SIGNAL(itemSelectionChanged()),this,SLOT(slot_itemSelectionChanged()));
}
void TestWidget::flushTreeWidget()
{
    QTreeWidgetItemIterator it(newTree);
    while (*it)
    {
        //QTreeWidgetItem是否满足条件---这里的条件可以自己修改
        if((*it)->text(1).contains(desLineedit->text())&&(*it)->text(4).contains(byteLineedit->text()))
        {
            (*it)->setHidden(false);
            QTreeWidgetItem *item = *it;
            //显示父节点
            while (item->parent())
            {
                item->parent()->setHidden(false);
                item = item->parent();
            }
        }
        else
        {
             //不满足满足条件先隐藏，它的子项目满足条件时会再次让它显示
            (*it)->setHidden(true);
        }
        ++it;
    }
}
void TestWidget::slot_searchText(QString str)
{
    Q_UNUSED(str);
    flushTreeWidget();
}
void TestWidget::slot_valueChange(QTreeWidgetItem* item,int column)
{
    int index=0;
    int comid =0;
    for(int i=0;i<GlobalConfig::recvTRDPList.length();++i)
    {
        if(item->parent()->text(0).toUInt()==GlobalConfig::recvTRDPList[i].comid)
            break;
        comid++;
    }
    index = item->text(4).toInt()*10+item->text(5).toInt();
    if(column == 6)
    {
//        qDebug()<<"value change is"<<item->text(column);
//        qDebug()<<"ComidCount change is"<<comid<<"IndexCount change is"<<index;
        emit valueChange(comid,index,item->text(column).toInt());
    }

}
void TestWidget::slot_itemDoubleClicked(QTreeWidgetItem *item, int column)
{

    if(column == 6)
    {
//        qDebug()<<"column"<<column<<"is double clicked";
        newTree->openPersistentEditor(item,column);
        m_item = item;
        m_column = column;
    }

}
void TestWidget::slot_itemSelectionChanged()
{
    if(m_item!=NULL)
        newTree->closePersistentEditor(m_item,m_column);
}
